import 'package:flutter/material.dart';

class Flutter_ParallelAnimation_Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Flutter_ParallelAnimation_State();
  }
}

class Flutter_ParallelAnimation_State
    extends State<Flutter_ParallelAnimation_Page>
    with SingleTickerProviderStateMixin {

  Animation<double> animation;
  AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        vsync: this, duration: Duration(milliseconds: 2000));
    animation = CurvedAnimation(parent: controller, curve: Curves.easeIn)
    //addStatusListener来监听动画状态更改的通知，AnimationStatus.forward开始，AnimationStatus.completed结束
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse(); //动画倒着执行
        } else if (status == AnimationStatus.dismissed) {
          controller.forward(); //动画开始执行
        }
      });

    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedLogo(animation: animation,);
  }

  @override
  void dispose() {
    super.dispose();
//    调用dispose()方法,以防止内存泄漏
    controller.dispose();
  }
}

class AnimatedLogo extends AnimatedWidget {
  static final _opacityTween = new Tween<double>(begin: 1.0, end: 0.3);
  static final _sizeTween = new Tween<double>(begin: 0.0, end: 300.0);

  AnimatedLogo({Key key, Animation<double>animation})
      :super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    Animation<double>animation = listenable;
    return Center(
        child: Opacity(
            opacity: _opacityTween.evaluate(animation),
            child: Container(
              color: Colors.white,
              width: _sizeTween.evaluate(animation),
              height: _sizeTween.evaluate(animation),
              child: FlutterLogo(),
            )
        )
    );
  }
}